import 'dart:convert';

class BuildParameters {
  // 基础参数
  String outputDir;
  String outputFilename;
  bool standalone;
  bool module;
  bool plugin;

  // 优化参数
  bool enableOptimizations;
  bool noAsserts;
  bool noDocstrings;
  String optimizationLevel; // O0, O1, O2

  // 包含/排除参数
  List<String> includePackages;
  List<String> excludePackages;
  List<String> includeModules;
  List<String> excludeModules;
  List<String> includeDataFiles;
  List<String> includeDataDirs;

  // 平台特定参数
  bool windowsDisableConsole;
  String windowsIconPath;
  String windowsVersionInfo;
  bool windowsUacAdmin;
  bool windowsUacUiAccess;

  // 调试和信息参数
  bool verbose;
  bool showProgress;
  bool showMemory;
  bool debugMode;
  bool traceExecution;

  // 高级参数
  bool followImports;
  bool followImportTo;
  bool noFollowImports;
  List<String> noFollowImportTo;
  bool enablePlugins;
  List<String> userPlugins;
  String pythonPath;
  String tempDir;

  // 性能参数
  int jobs; // 并行编译任务数
  bool lto; // Link Time Optimization
  bool pgoGenerate;
  String pgoUse;

  // 兼容性参数
  String pythonVersion;
  bool pythonDebug;
  bool pythonFlags;

  BuildParameters({
    this.outputDir = '',
    this.outputFilename = '',
    this.standalone = true,
    this.module = false,
    this.plugin = false,
    this.enableOptimizations = true,
    this.noAsserts = false,
    this.noDocstrings = false,
    this.optimizationLevel = 'O1',
    this.includePackages = const [],
    this.excludePackages = const [],
    this.includeModules = const [],
    this.excludeModules = const [],
    this.includeDataFiles = const [],
    this.includeDataDirs = const [],
    this.windowsDisableConsole = false,
    this.windowsIconPath = '',
    this.windowsVersionInfo = '',
    this.windowsUacAdmin = false,
    this.windowsUacUiAccess = false,
    this.verbose = false,
    this.showProgress = true,
    this.showMemory = false,
    this.debugMode = false,
    this.traceExecution = false,
    this.followImports = true,
    this.followImportTo = true,
    this.noFollowImports = false,
    this.noFollowImportTo = const [],
    this.enablePlugins = true,
    this.userPlugins = const [],
    this.pythonPath = '',
    this.tempDir = '',
    this.jobs = 0, // 0 表示自动检测
    this.lto = false,
    this.pgoGenerate = false,
    this.pgoUse = '',
    this.pythonVersion = '',
    this.pythonDebug = false,
    this.pythonFlags = false,
  });

  Map<String, dynamic> toJson() {
    return {
      'outputDir': outputDir,
      'outputFilename': outputFilename,
      'standalone': standalone,
      'module': module,
      'plugin': plugin,
      'enableOptimizations': enableOptimizations,
      'noAsserts': noAsserts,
      'noDocstrings': noDocstrings,
      'optimizationLevel': optimizationLevel,
      'includePackages': includePackages,
      'excludePackages': excludePackages,
      'includeModules': includeModules,
      'excludeModules': excludeModules,
      'includeDataFiles': includeDataFiles,
      'includeDataDirs': includeDataDirs,
      'windowsDisableConsole': windowsDisableConsole,
      'windowsIconPath': windowsIconPath,
      'windowsVersionInfo': windowsVersionInfo,
      'windowsUacAdmin': windowsUacAdmin,
      'windowsUacUiAccess': windowsUacUiAccess,
      'verbose': verbose,
      'showProgress': showProgress,
      'showMemory': showMemory,
      'debugMode': debugMode,
      'traceExecution': traceExecution,
      'followImports': followImports,
      'followImportTo': followImportTo,
      'noFollowImports': noFollowImports,
      'noFollowImportTo': noFollowImportTo,
      'enablePlugins': enablePlugins,
      'userPlugins': userPlugins,
      'pythonPath': pythonPath,
      'tempDir': tempDir,
      'jobs': jobs,
      'lto': lto,
      'pgoGenerate': pgoGenerate,
      'pgoUse': pgoUse,
      'pythonVersion': pythonVersion,
      'pythonDebug': pythonDebug,
      'pythonFlags': pythonFlags,
    };
  }

  factory BuildParameters.fromJson(Map<String, dynamic> json) {
    return BuildParameters(
      outputDir: json['outputDir'] ?? '',
      outputFilename: json['outputFilename'] ?? '',
      standalone: json['standalone'] ?? true,
      module: json['module'] ?? false,
      plugin: json['plugin'] ?? false,
      enableOptimizations: json['enableOptimizations'] ?? true,
      noAsserts: json['noAsserts'] ?? false,
      noDocstrings: json['noDocstrings'] ?? false,
      optimizationLevel: json['optimizationLevel'] ?? 'O1',
      includePackages: List<String>.from(json['includePackages'] ?? []),
      excludePackages: List<String>.from(json['excludePackages'] ?? []),
      includeModules: List<String>.from(json['includeModules'] ?? []),
      excludeModules: List<String>.from(json['excludeModules'] ?? []),
      includeDataFiles: List<String>.from(json['includeDataFiles'] ?? []),
      includeDataDirs: List<String>.from(json['includeDataDirs'] ?? []),
      windowsDisableConsole: json['windowsDisableConsole'] ?? false,
      windowsIconPath: json['windowsIconPath'] ?? '',
      windowsVersionInfo: json['windowsVersionInfo'] ?? '',
      windowsUacAdmin: json['windowsUacAdmin'] ?? false,
      windowsUacUiAccess: json['windowsUacUiAccess'] ?? false,
      verbose: json['verbose'] ?? false,
      showProgress: json['showProgress'] ?? true,
      showMemory: json['showMemory'] ?? false,
      debugMode: json['debugMode'] ?? false,
      traceExecution: json['traceExecution'] ?? false,
      followImports: json['followImports'] ?? true,
      followImportTo: json['followImportTo'] ?? true,
      noFollowImports: json['noFollowImports'] ?? false,
      noFollowImportTo: List<String>.from(json['noFollowImportTo'] ?? []),
      enablePlugins: json['enablePlugins'] ?? true,
      userPlugins: List<String>.from(json['userPlugins'] ?? []),
      pythonPath: json['pythonPath'] ?? '',
      tempDir: json['tempDir'] ?? '',
      jobs: json['jobs'] ?? 0,
      lto: json['lto'] ?? false,
      pgoGenerate: json['pgoGenerate'] ?? false,
      pgoUse: json['pgoUse'] ?? '',
      pythonVersion: json['pythonVersion'] ?? '',
      pythonDebug: json['pythonDebug'] ?? false,
      pythonFlags: json['pythonFlags'] ?? false,
    );
  }

  String toJsonString() {
    return jsonEncode(toJson());
  }

  factory BuildParameters.fromJsonString(String jsonString) {
    return BuildParameters.fromJson(jsonDecode(jsonString));
  }

  BuildParameters copyWith({
    String? outputDir,
    String? outputFilename,
    bool? standalone,
    bool? module,
    bool? plugin,
    bool? enableOptimizations,
    bool? noAsserts,
    bool? noDocstrings,
    String? optimizationLevel,
    List<String>? includePackages,
    List<String>? excludePackages,
    List<String>? includeModules,
    List<String>? excludeModules,
    List<String>? includeDataFiles,
    List<String>? includeDataDirs,
    bool? windowsDisableConsole,
    String? windowsIconPath,
    String? windowsVersionInfo,
    bool? windowsUacAdmin,
    bool? windowsUacUiAccess,
    bool? verbose,
    bool? showProgress,
    bool? showMemory,
    bool? debugMode,
    bool? traceExecution,
    bool? followImports,
    bool? followImportTo,
    bool? noFollowImports,
    List<String>? noFollowImportTo,
    bool? enablePlugins,
    List<String>? userPlugins,
    String? pythonPath,
    String? tempDir,
    int? jobs,
    bool? lto,
    bool? pgoGenerate,
    String? pgoUse,
    String? pythonVersion,
    bool? pythonDebug,
    bool? pythonFlags,
  }) {
    return BuildParameters(
      outputDir: outputDir ?? this.outputDir,
      outputFilename: outputFilename ?? this.outputFilename,
      standalone: standalone ?? this.standalone,
      module: module ?? this.module,
      plugin: plugin ?? this.plugin,
      enableOptimizations: enableOptimizations ?? this.enableOptimizations,
      noAsserts: noAsserts ?? this.noAsserts,
      noDocstrings: noDocstrings ?? this.noDocstrings,
      optimizationLevel: optimizationLevel ?? this.optimizationLevel,
      includePackages: includePackages ?? this.includePackages,
      excludePackages: excludePackages ?? this.excludePackages,
      includeModules: includeModules ?? this.includeModules,
      excludeModules: excludeModules ?? this.excludeModules,
      includeDataFiles: includeDataFiles ?? this.includeDataFiles,
      includeDataDirs: includeDataDirs ?? this.includeDataDirs,
      windowsDisableConsole:
          windowsDisableConsole ?? this.windowsDisableConsole,
      windowsIconPath: windowsIconPath ?? this.windowsIconPath,
      windowsVersionInfo: windowsVersionInfo ?? this.windowsVersionInfo,
      windowsUacAdmin: windowsUacAdmin ?? this.windowsUacAdmin,
      windowsUacUiAccess: windowsUacUiAccess ?? this.windowsUacUiAccess,
      verbose: verbose ?? this.verbose,
      showProgress: showProgress ?? this.showProgress,
      showMemory: showMemory ?? this.showMemory,
      debugMode: debugMode ?? this.debugMode,
      traceExecution: traceExecution ?? this.traceExecution,
      followImports: followImports ?? this.followImports,
      followImportTo: followImportTo ?? this.followImportTo,
      noFollowImports: noFollowImports ?? this.noFollowImports,
      noFollowImportTo: noFollowImportTo ?? this.noFollowImportTo,
      enablePlugins: enablePlugins ?? this.enablePlugins,
      userPlugins: userPlugins ?? this.userPlugins,
      pythonPath: pythonPath ?? this.pythonPath,
      tempDir: tempDir ?? this.tempDir,
      jobs: jobs ?? this.jobs,
      lto: lto ?? this.lto,
      pgoGenerate: pgoGenerate ?? this.pgoGenerate,
      pgoUse: pgoUse ?? this.pgoUse,
      pythonVersion: pythonVersion ?? this.pythonVersion,
      pythonDebug: pythonDebug ?? this.pythonDebug,
      pythonFlags: pythonFlags ?? this.pythonFlags,
    );
  }

  /// 生成Nuitka命令行参数
  List<String> generateCommandLineArgs(String inputFile) {
    List<String> args = ['nuitka'];

    // 输入文件
    args.add(inputFile);

    // 构建模式
    if (standalone) args.add('--standalone');
    if (module) args.add('--module');
    if (plugin) args.add('--plugin-enable=all');

    // 输出设置
    if (outputDir.isNotEmpty) {
      args.addAll(['--output-dir', outputDir]);
    }
    if (outputFilename.isNotEmpty) {
      args.addAll(['--output-filename', outputFilename]);
    }

    // 优化参数
    if (enableOptimizations) args.add('--enable-optimizations');
    if (noAsserts) args.add('--no-asserts');
    if (noDocstrings) args.add('--no-docstrings');
    args.add('-$optimizationLevel');

    // 包含/排除
    for (String pkg in includePackages) {
      args.addAll(['--include-package', pkg]);
    }
    for (String pkg in excludePackages) {
      args.addAll(['--nofollow-import-to', pkg]);
    }
    for (String mod in includeModules) {
      args.addAll(['--include-module', mod]);
    }

    // Windows特定参数
    if (windowsDisableConsole) args.add('--windows-disable-console');
    if (windowsIconPath.isNotEmpty) {
      args.addAll(['--windows-icon-from-ico', windowsIconPath]);
    }
    if (windowsUacAdmin) args.add('--windows-uac-admin');

    // 调试参数
    if (verbose) args.add('--verbose');
    if (showProgress) args.add('--show-progress');
    if (showMemory) args.add('--show-memory');
    if (debugMode) args.add('--debug');

    // 导入跟踪
    if (followImports) args.add('--follow-imports');
    if (noFollowImports) args.add('--nofollow-imports');

    // 性能参数
    if (jobs > 0) args.addAll(['--jobs', jobs.toString()]);
    if (lto) args.add('--lto=yes');

    return args;
  }
}
